package com.sprite.htr.spritehtr;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.sprite.htr.spritehtr.adapter.SubViewAdapter;
import com.sprite.htr.spritehtr.util.AnimUtil;
import com.sprite.htr.spritehtr.view.HomeView;
import com.sprite.htr.spritehtr.view.ShopView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {
    private ImageView iv_message;
    private ImageView iv_shouye;
    private ImageView iv_friends;
    private ImageView iv_shop;
    private ImageView iv_setting;
    private ImageView iv_lever;
    private ImageView iv_raid;
    private FrameLayout fl_beibao;
    private FrameLayout fl_flod;
    private FrameLayout fl_rongyu;
    private FrameLayout fl_liuyan;
    private FrameLayout fl_renwu;
    private FrameLayout fl_chuzhan;
    private FrameLayout fl_hecheng;
    private FrameLayout fl_fold;
    private ProgressBar pb_timer_1;
    private RelativeLayout rl_main_activity;
    private List<View> subViewList;
    private ViewPager vp_content;
    private LinearLayout ll_sub_btns;
    private ShopView shopView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        InitUI();
        updatTimer();
        initSubViewList();

    }

    private void updatTimer() {
        pb_timer_1=(ProgressBar)findViewById(R.id.pb_timer_l);
        new Thread(){
            public void run(){
                while(true){
                    int progress=pb_timer_1.getProgress();
                    if(progress==100){
                        progress=-10;
                    }
                    try{
                         Thread.sleep(1000);
                    }
                    catch (Exception e){

                    }
                    pb_timer_1.setProgress(progress+10);
                }
            }
        }.start();
    }
    private void initSubViewList() {
        subViewList = new ArrayList<View>();
        HomeView homeview = new HomeView(getApplicationContext());
        ShopView shopView = new ShopView(getApplicationContext());

        subViewList.add(homeview.getView());
        subViewList.add(shopView.getView());

        SubViewAdapter adapter = new SubViewAdapter(getApplicationContext(), subViewList);
        vp_content.setAdapter(adapter);
    }
    private void InitUI() {
        rl_main_activity=(RelativeLayout)findViewById(R.id.rl_main_activity) ;
        vp_content = (ViewPager) findViewById(R.id.vp_content);

        vp_content.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });


        iv_message=(ImageView)findViewById(R.id.iv_message);
        AnimUtil.addOnTouchListener(iv_message,0f,0f,null);

        iv_friends=(ImageView)findViewById(R.id.iv_friends);
        AnimUtil.addOnTouchListener(iv_friends,0.5f,1f,animListener);
        iv_shouye=(ImageView)findViewById(R.id.iv_home);
        AnimUtil.addOnTouchListener(iv_shouye,0.5f,1f,animListener);
        iv_lever=(ImageView)findViewById(R.id.iv_level);
        AnimUtil.addOnTouchListener(iv_lever,0.5f,1f,animListener);
        iv_raid=(ImageView)findViewById(R.id.iv_raid);
        AnimUtil.addOnTouchListener(iv_raid,0.5f,1f,animListener);
        iv_setting=(ImageView)findViewById(R.id.iv_setting);
        AnimUtil.addOnTouchListener(iv_setting,0.5f,1f,animListener);
        iv_shop=(ImageView)findViewById(R.id.iv_shop);
        AnimUtil.addOnTouchListener(iv_shop,0.5f,1f,animListener);
        fl_beibao=(FrameLayout) findViewById(R.id.fl_beibao);
        AnimUtil.addOnTouchListener(fl_beibao,0.5f,1f,null);
        fl_renwu=(FrameLayout) findViewById(R.id.fl_renwu);
        AnimUtil.addOnTouchListener(fl_renwu,0.5f,1f,null);
        fl_rongyu=(FrameLayout) findViewById(R.id.fl_rongyu);
        AnimUtil.addOnTouchListener(fl_rongyu,0.5f,1f,null);
        fl_liuyan=(FrameLayout) findViewById(R.id.fl_liuyan);
        AnimUtil.addOnTouchListener(fl_liuyan,0.5f,1f,null);
        fl_chuzhan=(FrameLayout) findViewById(R.id.fl_chuzhan);
        AnimUtil.addOnTouchListener(fl_chuzhan,0.5f,1f,null);
        fl_hecheng=(FrameLayout) findViewById(R.id.fl_hecheng);
        AnimUtil.addOnTouchListener(fl_hecheng,0.5f,1f,null);
        fl_beibao=(FrameLayout) findViewById(R.id.fl_beibao);
        AnimUtil.addOnTouchListener(fl_beibao,0.5f,1f,null);
        fl_fold=(FrameLayout)findViewById(R.id.fl_fold);
        String[] proNames = new String[]{AnimUtil.PROPERTY_SCALEX, AnimUtil.PROPERTY_SCALEY};
        AnimUtil.addOnTouchListener(fl_fold, proNames,
                new float[]{1f, 0.75f}, new float[]{0.75f, 1f}, animListener);
         ll_sub_btns= (LinearLayout) findViewById(R.id.ll_sub_btn);

        pb_timer_1 = (ProgressBar) findViewById(R.id.pb_timer_l);

    }

    private AnimUtil.AnimListener animListener=new AnimUtil.AnimListener() {
        boolean isDown;
        @Override

        public void move(View view) {

        }

        @Override
        public void down(View view) {

        }

        @Override
        public void up(View view) {
            switch (view.getId()) {
                case R.id.iv_shop:
                    Log.i("MainActivity", "shop");
                    if (vp_content.getCurrentItem() != 1) {
                        shopView.show();
                    }
                    vp_content.setCurrentItem(1);
                    rl_main_activity.setBackgroundResource(R.mipmap.bg_shop);

                    break;
                case R.id.iv_home:
                    Log.i("MainActivity", "shouye");
                    addAnimationListener(iv_shouye);
                    break;
                case R.id.fl_fold:
                    int subHeight = ll_sub_btns.getHeight() - 52;
                    if (isDown) {
                        AnimUtil.addObjectAnimation(view, new String[]{AnimUtil.PROPERTY_TRANSLATIONY, AnimUtil.PROPERTY_ROTATIONX,
                                AnimUtil.PROPERTY_ROTATIONY}, 0f);
                        AnimUtil.addObjectAnimation(view, new String[]{AnimUtil.PROPERTY_ROTATIONX,
                                AnimUtil.PROPERTY_ROTATIONY}, 180f, 3600f);
                        AnimUtil.addObjectAnimation(ll_sub_btns, new String[]{AnimUtil.PROPERTY_TRANSLATIONY}, 0f);
                    } else {
                        AnimUtil.addObjectAnimation(view, new String[]{AnimUtil.PROPERTY_TRANSLATIONY, AnimUtil.PROPERTY_ROTATIONX,
                                AnimUtil.PROPERTY_ROTATIONY}, subHeight);
                        AnimUtil.addObjectAnimation(view, new String[]{AnimUtil.PROPERTY_ROTATIONX,
                                AnimUtil.PROPERTY_ROTATIONY}, 0f, 180f);
                        AnimUtil.addObjectAnimation(ll_sub_btns, new String[]{AnimUtil.PROPERTY_TRANSLATIONY}, subHeight);
                    }
                    isDown = !isDown;
                    break;
            }
        }
    };
    public void addAnimationListener(final View v) {
        shopView.hide(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                switch (v.getId()) {
                    case R.id.iv_home:
                        vp_content.setCurrentItem(0);
                        rl_main_activity.setBackgroundResource(R.mipmap.background);
                        break;
                }
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }
}
